import navs from "../../modules/navs.ts";
import { defineStore } from 'pinia';
import { checkPermissions } from "../../modules/permission.ts";

interface INavsState {}

const useNavsStore = defineStore('navs', {
    state: (): INavsState => ({

    }),
    getters: {
        items(): any[]{
            const handler = function(items: any[]) {
                let res = [];
                for (let i=0; i < items.length; i++){
                    //这里用对象复制,防止后续操作时污染
                    const item = {...items[i]};
                    //进行权限判断
                    if (item?.permissions) {
                        if(!checkPermissions(item?.permissions)) {
                            continue;
                        }
                    }
                    //判断是否对子列表进行递归
                    if (item?.childList) {
                        item.childList = handler(item.childList);
                    }
                    //加入到返回集合中
                    res.push(item);
                }
                return res;
            };
            return handler(navs);
        }
    },
    actions: {

    },
});

export default useNavsStore;
